package com.sms.sendservice.config;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import jakarta.annotation.PostConstruct;
import org.springframework.context.annotation.Configuration;
import java.util.ArrayList;
import java.util.List;

@Configuration
public class SentinelConfig {

    @PostConstruct
    public void initFlowRules() {
        List<FlowRule> rules = new ArrayList<>();

        // 1. 全局限流规则（对应默认资源名）
        FlowRule globalRule = new FlowRule();
        globalRule.setResource("user-service-global"); // 与注解中的resource对应
        globalRule.setGrade(RuleConstant.FLOW_GRADE_QPS); // 基于QPS限流
        globalRule.setCount(1000); // 全局QPS阈值1000
        rules.add(globalRule);

        // 2. 特定接口限流规则（可选）
        FlowRule detailRule = new FlowRule();
        detailRule.setResource("user-service-detail");
        detailRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        detailRule.setCount(500); // 该接口单独QPS阈值500
        rules.add(detailRule);

        FlowRuleManager.loadRules(rules);
    }
}
